import { createRouter, createWebHistory } from 'vue-router'

export const routes = [
    {
      path:"/",
      name:'home',
      redirect:'/newspool',
      component:()=>import('@/views/HomeView.vue'),
      children:[
        {
          path:"/newspool",
          name:'newspool',
          menu:'新闻池',
          component:()=>import('@/views/navs/NewsPool.vue')
        },
        {
          path:"/news-publish",
          name:'newspublish',
          menu:'新闻发布',
          component:()=>import('@/views/navs/NewsPublish.vue')
        },
        {
          path:"/news-published",
          menu:'已发布新闻',
          component:()=>import('@/views/navs/NewsPublished.vue')
        },
        {
          path:"/system-management",
          menu:'系统管理',
          component:()=>import('@/views/navs/SystemManagement.vue')
        },
        {
          path:"/log-management",
          menu:'日志管理',
          component:()=>import('@/views/navs/LogManagement.vue')
        },
        {
          path:"/notification-management",
          menu:'通知管理',
          component:()=>import('@/views/navs/NotificationManagement.vue')
        },
        {
          path:"/internation-management",
          menu:'国际化管理',
          component:()=>import('@/views/navs/InternationManagement.vue')
        },
      ]
    },
    {
      path:"/login",
      name:'login',
      component:()=>import('@/views/LoginView.vue')
    },
    {
      path:"/preview",
      name:'preview',
      component:()=>import('@/views/PreviewView.vue')
    }
  ]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach((to,from)=>{
  const authenticated=()=>{
    const loginInfo=localStorage.getItem('login')
    return !!loginInfo
  }
  if(to.path!=='/login'&&!authenticated()){
    return {path:'/login'}
  }
  if(to.path==='/login'&&authenticated()){
    return {path:'/newspool'}
  }
  return true

})

export default router
